﻿using AntdUI;
using WenAntdUI.Settings;

namespace WenAntdUI.Helps
{
    public class AntdHelp
    {

        readonly static HashSet<Window> subWindows = [];

        /// <summary>
        /// 设置主题
        /// </summary>
        /// <param name="window"></param>
        /// <param name="btnSwitch"></param>
        public static void SetTheme(Window window, AntdUI.Button? btnSwitch)
        {
            bool dark = AppSettings.GetBool("ThemeDark");
            Color primary = AppSettings.Get<Color>("ThemeColor");
            if (primary.Name == "0")
            {
                AppSettings.Set("ThemeColor", Color.FromArgb(105, 70, 255));
                primary = Color.FromArgb(105, 70, 255);
            }
            Config.IsDark = dark;
            if (dark)
            {
                if (btnSwitch != null) btnSwitch.IconSvg = "MoonFilled";
                window.BackColor = Color.Black;
                window.ForeColor = Color.White;
            }
            else
            {
                if (btnSwitch != null) btnSwitch.IconSvg = "SunOutlined";
                window.BackColor = Color.White;
                window.ForeColor = Color.Black;
            }
            Style.SetPrimary(primary);
        }

        /// <summary>
        /// 设置主题
        /// </summary>
        /// <param name="dark"></param>
        public static void SetTheme(Window window, AntdUI.Button? btnSwitch, bool dark, Color primary)
        {
            Config.IsDark = dark;
            if (dark)
            {
                if (btnSwitch != null) btnSwitch.IconSvg = "MoonFilled";
                window.BackColor = Color.Black;
                window.ForeColor = Color.White;
            }
            else
            {
                if (btnSwitch != null) btnSwitch.IconSvg = "SunOutlined";
                window.BackColor = Color.White;
                window.ForeColor = Color.Black;
            }
            Style.SetPrimary(primary);
            window.Refresh();
            BroadcastThemeToSubWindows();
            AppSettings.Set("ThemeDark", dark);
            AppSettings.Set("ThemeColor", "#" + primary.ToHex());
        }

        /// <summary>
        /// 设置主题色
        /// </summary>
        /// <param name="primary"></param>
        public static void SetThemeColor(Window window, Color primary)
        {
            Style.SetPrimary(primary);
            window.Refresh();
            BroadcastThemeToSubWindows();
            AppSettings.Set("ThemeColor", primary.ToString());
        }

        /// <summary>
        /// 继承主窗体的主题
        /// </summary>
        public static void InheritTheme(Window window)
        {
            bool themeChanged = false;
            bool dark = Config.IsDark;
            if (dark)
            {
                if (window.BackColor != Color.Black)
                {
                    window.BackColor = Color.Black;
                    window.ForeColor = Color.White;
                    themeChanged = true;
                }
            }
            else
            {
                if (window.BackColor != Color.White)
                {
                    window.BackColor = Color.White;
                    window.ForeColor = Color.Black;
                    themeChanged = true;
                }
            }
            if (themeChanged) window.Refresh();
            subWindows.Add(window);
        }

        /// <summary>
        /// 把主题应用到所有子窗体
        /// </summary>
        private static void BroadcastThemeToSubWindows()
        {
            foreach (Window subWin in subWindows)
            {
                if (subWin != null && !subWin.IsDisposed && subWin.Visible)
                {
                    InheritTheme(subWin);
                    subWin.Refresh();
                }
            }
        }



        /// <summary>
        /// 设置呼吸灯动效
        /// </summary>
        /// <param name="ctl"></param>
        /// <param name="color1"></param>
        /// <param name="color2"></param>
        /// <param name="animationType"></param>
        public static void SetBreathingLight(Control ctl, Color color1, Color color2, int animationType = 1)
        {
            AntdUI.Button? btn = null;

            if (ctl is AntdUI.Button _btn)
            {
                btn = _btn;
                if (btn.Type == TTypeMini.Default) return;
            }

            AntdUI.Label? lb = null;

            if (ctl is AntdUI.Label _lb)
            {
                lb = _lb;
            }

            Random rnd = new();
            int angle = 90;
            if (color1 == Color.Empty) color1 = Color.FromArgb(rnd.Next(100, 200), rnd.Next(100, 200), rnd.Next(100, 200));
            if (color2 == Color.Empty) color2 = Color.FromArgb(rnd.Next(100, 200), rnd.Next(100, 200), rnd.Next(100, 200));

            if (btn != null)
            {
                btn.BackExtend = $"{angle},{ColorTranslator.ToHtml(color1)},{ColorTranslator.ToHtml(color2)}";
            }
            else if (lb != null)
            {
                lb.ColorExtend = $"{angle},{ColorTranslator.ToHtml(color1)},{ColorTranslator.ToHtml(color2)}";
            }

            int i = 0;
            int ii = 1;

            System.Windows.Forms.Timer timer = new() { Interval = 10 };

            timer.Tick += (s, e) =>
            {
                i += ii;

                if (i == 50) ii = -1;
                if (i == -50) ii = 1;

                Color color3 = color1;
                Color color4 = color2;

                int r, g, b;

                if (animationType == 1)
                {
                    r = color1.R + i;
                    if (r > 255) r = 255; else if (r < 0) r = 0;
                    g = color1.G + i;
                    if (g > 255) g = 255; else if (g < 0) g = 0;
                    b = color1.B + i;
                    if (b > 255) b = 255; else if (b < 0) b = 0;

                    color3 = Color.FromArgb(r, g, b);

                    r = color2.R + i;
                    if (r > 255) r = 255; else if (r < 0) r = 0;
                    g = color2.G + i;
                    if (g > 255) g = 255; else if (g < 0) g = 0;
                    b = color2.B + i;
                    if (b > 255) b = 255; else if (b < 0) b = 0;

                    color4 = Color.FromArgb(r, g, b);
                }
                else if (animationType == 2)
                {
                    r = color1.R + i;
                    if (r > 255) r = 255; else if (r < 0) r = 0;
                    g = color1.G;
                    b = color2.B;

                    color3 = Color.FromArgb(color1.R + i, color1.G, color1.B);

                    r = color1.R;
                    g = color1.G + i;
                    if (g > 255) g = 255; else if (g < 0) g = 0;
                    b = color2.B;

                    color4 = Color.FromArgb(color2.R, color2.G + i, color2.B);
                }

                if (btn != null)
                {
                    btn.BackExtend = $"{angle},{ColorTranslator.ToHtml(color3)},{ColorTranslator.ToHtml(color4)}";
                }
                else if (lb != null)
                {
                    lb.ColorExtend = $"{angle},{ColorTranslator.ToHtml(color3)},{ColorTranslator.ToHtml(color4)}";
                }
            };
            timer.Start();
        }
    }
}